package com.example.cafe.work.config;



import com.example.cafe.work.dto.UserSenstive;
import com.example.cafe.work.util.UserHolder;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断UserHolder中是否存在用户信息
        UserSenstive user = UserHolder.getUser();
        System.out.println(user);
        if (UserHolder.getUser() == null){
            response.setStatus(401);
            return false;
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //把ThreadLocal中的用户信息移除掉
        UserHolder.removeUser();
    }
}
